﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid;
using BusinessObjects;
using DataAccess;

namespace QuanLyHopDong
{
    public partial class TinhTrangListView : Form
    {
        private BindingList<TinhTrang> bindingListTinhTrang;
        private TinhTrang _tinhTrang;
        
        public TinhTrangListView()
        {
            InitializeComponent();
        }

        private void gcTinhTrang_Load(object sender, EventArgs e)
        {
            string[] listFieldName = new string[] { "MaQuanLy", "TenTinhTrang" };
            string[] listColumnHeader = new string[] { "Mã Quản Lý", "Tên Tình Trạng" };
            AppGridView.InitGridView(gvTinhTrang, false, false, DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, false, false, true);
            AppGridView.ReadOnlyGridView(gvTinhTrang);
            AppGridView.ShowField(gvTinhTrang, listFieldName, listColumnHeader);

            LoadData();
        }

        #region User Functions
        void LoadData()
        {
            List<TinhTrang> list = DataAccessObject.TinhTrang.GetListTinhTrang();
            bindingListTinhTrang = new BindingList<TinhTrang>(list);

            bindingSouceTinhTrang.DataSource = bindingListTinhTrang;
            gvTinhTrang.Invalidate();
        }

        void OpenTinhTrangDetailView(TinhTrangDetailView tinhTrangDetailView)
        {
            tinhTrangDetailView.SaveChanged += new EventHandler(tinhTrangDetailView_SaveChanged);
            tinhTrangDetailView.DeleteChanged += new EventHandler(tinhTrangDetailView_DeleteChanged);
            tinhTrangDetailView.ShowDialog();

        }

        void tinhTrangDetailView_DeleteChanged(object sender, EventArgs e)
        {
            DeleteRow();
        }

        void tinhTrangDetailView_SaveChanged(object sender, EventArgs e)
        {
            bindingListTinhTrang.Add(_tinhTrang);
            gvTinhTrang.Invalidate();
        }
        TinhTrang GetSelectedRow()
        {
            TinhTrang tinhTrang = new TinhTrang();
            int rowSelected = gvTinhTrang.FocusedRowHandle;
            tinhTrang = gvTinhTrang.GetRow(rowSelected) as TinhTrang;
            return tinhTrang;
        }
        void DeleteRow()
        {
            if (MessageBox.Show("Bạn có chắc xóa?", "Xóa một dòng thông tin học kỳ", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                _tinhTrang = gvTinhTrang.GetFocusedRow() as TinhTrang;

                if (_tinhTrang != null && _tinhTrang.Oid > 0)
                    if (DataAccessObject.TinhTrang.Delete(_tinhTrang) > 0)
                    {
                        gvTinhTrang.DeleteRow(gvTinhTrang.FocusedRowHandle);
                        gvTinhTrang.Invalidate();
                    }
                    else
                        MessageBox.Show("Không xóa được", "Có Lỗi Xảy Ra", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _tinhTrang = new TinhTrang();
            
            TinhTrangDetailView tinhTrangDetailView = new TinhTrangDetailView(_tinhTrang);
            OpenTinhTrangDetailView(tinhTrangDetailView);
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc xóa?", "Lưu ý", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.No)
                return;

            _tinhTrang = GetSelectedRow();

            if (DataAccessObject.TinhTrang.Delete(_tinhTrang) > 0)
            {
                gvTinhTrang.DeleteRow(gvTinhTrang.FocusedRowHandle);
                gvTinhTrang.Invalidate();
            }
        }

        private void btnDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void gcTinhTrang_DoubleClick(object sender, EventArgs e)
        {
            _tinhTrang = GetSelectedRow();
            TinhTrangDetailView tinhTrangDetailView = new TinhTrangDetailView(_tinhTrang);
            OpenTinhTrangDetailView(tinhTrangDetailView);
        }

        private void btnLamMoi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadData();
        }
    }
}
